package it.danghongphat.gui;

import it.danghongphat.gui.component.ContractTree;
import it.danghongphat.gui.windowms.DaukyGui;
import it.danghongphat.gui.windowms.MaterialGui;
import it.danghongphat.list.ListDauky;
import it.danghongphat.list.ListMaterial;
import it.danghongphat.object.Material;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Documented;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

import com.javadocking.DockingManager;
import com.javadocking.component.DockHeader;
import com.javadocking.component.PointDockHeader;
import com.javadocking.dock.BorderDock;
import com.javadocking.dock.CompositeLineDock;
import com.javadocking.dock.FloatDock;
import com.javadocking.dock.GridDock;
import com.javadocking.dock.LeafDock;
import com.javadocking.dock.LineDock;
import com.javadocking.dock.Position;
import com.javadocking.dock.SplitDock;
import com.javadocking.dock.TabDock;
import com.javadocking.dock.docker.BorderDocker;
import com.javadocking.dock.factory.CompositeToolBarDockFactory;
import com.javadocking.dock.factory.LeafDockFactory;
import com.javadocking.dock.factory.ToolBarDockFactory;
import com.javadocking.dockable.ButtonDockable;
import com.javadocking.dockable.DefaultDockable;
import com.javadocking.dockable.Dockable;
import com.javadocking.dockable.DockableState;
import com.javadocking.dockable.DockingMode;
import com.javadocking.dockable.DraggableContent;
import com.javadocking.dockable.StateActionDockable;
import com.javadocking.dockable.action.DefaultDockableStateAction;
import com.javadocking.dockable.action.DefaultDockableStateActionFactory;
import com.javadocking.drag.DragListener;
import com.javadocking.event.DockingEvent;
import com.javadocking.event.DockingListener;
import com.javadocking.model.DefaultDockingPath;
import com.javadocking.model.DockModel;
import com.javadocking.model.DockingPath;
import com.javadocking.model.DockingPathModel;
import com.javadocking.model.FloatDockModel;
import com.javadocking.model.codec.DockModelPropertiesDecoder;
import com.javadocking.model.codec.DockModelPropertiesEncoder;
import com.javadocking.util.Find;
import com.javadocking.util.LAF;
import com.javadocking.util.LookAndFeelUtil;
import com.javadocking.util.Picture;
import com.javadocking.util.SampleComponentFactory;
import com.javadocking.util.ToolBarButton;
import com.javadocking.visualizer.DockingMinimizer;
import com.javadocking.visualizer.Externalizer;
import com.javadocking.visualizer.FloatExternalizer;
import com.javadocking.visualizer.SingleMaximizer;
import com.javadocking.visualizer.Visualizer;
public class MainForm extends JPanel {
	private static final long serialVersionUID = 1L;
	public static final int 			FRAME_WIDTH 			= 900;
	public static final int 			FRAME_HEIGHT 			= 650;
	public static final String 			SOURCE 					= "workspace_1_5.dck";
	public static final String 			CENTER_DOCKING_PATH_ID  = "centerDockingPathId";
	public static LAF[] 				LAFS;
	static int newDockableCount = 0;
	public FloatDock floatDock;
	public GridDock toolGridDock;
	public ListMaterial listmaterial;
	public ListDauky listdauky;
	// Fields.

	/** The ID for the owner window. */
	private String 						frameId 		= "frame";
	/** The model with the docks, dockables, and visualizers (a minimizer and a maximizer). */
	private FloatDockModel 				dockModel;
	/** All the dockables of the application. */
	private Dockable[] dockables;
	/** All the dockables for buttons of the application. */
	private Dockable[] buttonDockables;
	Dockable[] buttonDock;
	MaterialGui hh;
	DaukyGui dk;
	boolean mok=false;
	boolean mokdauky=false;
	boolean mokhanhoa=false;
	
	// Constructors.

	public MainForm(JFrame frame)
	{
		super(new BorderLayout());
		hh=new MaterialGui(this);
		dk=new DaukyGui(this);
		dockModel = new FloatDockModel();
		dockModel.addOwner("frame0", frame);
		floatDock = dockModel.getFloatDock(frame);
		floatDock.setChildDockFactory(new LeafDockFactory(false));
		toolGridDock = new GridDock(DockingMode.TOOL_GRID);
		buttonDock = new Dockable[2];
		buttonDock[0] = createButtonDockable1("ButtonDockCancel","Delete",new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/cancel.png")),"Delete!");
		buttonDock[1] = createButtonDockable1("ButtonDockableBookOpen","Print",new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/book_open.png")),"Print!");
		toolGridDock.addDockable(buttonDock[0], new Position(0));
		toolGridDock.addDockable(buttonDock[1], new Position(1));
		listmaterial=new ListMaterial(this);
		listdauky=new ListDauky(this);
		// Set our custom component factory.
		DockingManager.setComponentFactory(new WorkspaceComponentFactory());

		// Create a maximizer.
		SingleMaximizer maximizer = new SingleMaximizer();
		
		// Create a minimizer.
		BorderDocker borderDocker = new BorderDocker();
		DockingMinimizer minimizer = new DockingMinimizer(borderDocker);
		
		// Add an externalizer to the dock model.
		Externalizer externalizer = new FloatExternalizer(frame);
		
		ContractTree performTree = new ContractTree();
		
		Find find = new Find();
		Picture sales = new Picture(new ImageIcon(getClass().getResource("/com/javadocking/resources/images/salesSm.gif")));

		// The arrays for the dockables and button dockables.
		dockables = new Dockable[5];
		

		
		// Create the dockables around the content components.
		dockables[0] = createDockable("Danh Mục Đầu Kỳ", 	 listdauky,      "Danh Mục Đầu Kỳ",  new ImageIcon(getClass().getResource("/com/javadocking/resources/images/text12.gif")),     "<html>De Bello Gallico: Liber 1<br><i>Gaius Julius Caesar</i><html>");
		dockables[1] = createDockable("Danh Mục Hàng Hóa",   listmaterial,      "Danh Mục Hàng Hóa",   new ImageIcon(getClass().getResource("/com/javadocking/resources/images/table12.gif")),    "Table with hello");
		dockables[2] = createDockable("Tác Vụ",performTree,"Tác Vụ",new ImageIcon(getClass().getResource("/com/javadocking/resources/images/person12.gif")),   "Sales Contacts");
		dockables[3]= createDockable("Sales",   sales,      "Sales",   new ImageIcon(getClass().getResource("/com/javadocking/resources/images/graph12.gif")),    "Sales of Books and CDs");
		dockables[4]= createDockable("Words",    find,   "Words",   new ImageIcon(getClass().getResource("/com/javadocking/resources/images/table12.gif")),    "Roman Gods");

		// The dockable with the find panel may not be maximized.
		((DefaultDockable)dockables[2]).setPossibleStates(DockableState.CLOSED | DockableState.NORMAL | DockableState.MINIMIZED | DockableState.EXTERNALIZED);
		
//		 Add actions to the dockables.// cho phep cac thao tac max, min, close cho panel
		for (int index = 0; index < dockables.length; index++)
		{
			if (index == 4)
			{
				// All actions, except the maximize.
				dockables[index] = addLimitActions(dockables[index]);
			}
			else
			{
				// All actions.
				dockables[index] = addAllActions(dockables[index]);
			}
		}
		performTree.tree.addTreeSelectionListener(new TreeSelectionListener() {
			
			@Override
			public void valueChanged(TreeSelectionEvent e) {
				 TreePath newPath = e.getNewLeadSelectionPath();  
			        DefaultMutableTreeNode newNode = null;  
			        if(newPath != null)  
			            newNode = (DefaultMutableTreeNode)newPath.getLastPathComponent();  
			        if(newNode != null){  
			        	if(newNode.toString()=="Danh mục hàng hóa"){
			        		if(mok==false){
			        			mok=true;
			        			floatDock.addChildDock(toolGridDock, new Position(600,50));
			        		}
			        		mokhanhoa=true;
		        			mokdauky=false;
			        		 Action restoreAction = new DefaultDockableStateAction(dockables[1], DockableState.NORMAL);
			        		 restoreAction.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Restore"));
			        		 
			        	}
			        	if(newNode.toString()=="Thông tin đầu kỳ"){
			        		if(mok==false){
			        			mok=true;
			        			floatDock.addChildDock(toolGridDock, new Position(600,50));
			        		}
			        		mokhanhoa=false;
		        			mokdauky=true;
			        		 Action restoreAction = new DefaultDockableStateAction(dockables[0], DockableState.NORMAL);
			        		 restoreAction.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Restore"));
			        		 
			        	}
			        }				
			}
		});
		buttonDockables = new Dockable[42];
		// Create the buttons with a dockable around.
		buttonDockables[0]  = createButtonDockable("ButtonDockableAdd",             "Add",               new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/add.png")),               "Add!");
		buttonDockables[1]  = createButtonDockable("ButtonDockableAccept",          "Accept",            new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/accept.png")),            "Accept!");
		buttonDockables[2]  = createButtonDockable("ButtonDockableCancel",          "Cancel",            new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/cancel.png")),            "Cancel!");

		buttonDockables[3]  = createButtonDockable("ButtonDockableUndo",            "Undo",              new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/undo.png")),              "Undo!");
		buttonDockables[4]  = createButtonDockable("ButtonDockableRedo",            "Redo",              new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/redo.png")),              "Redo!");
		buttonDockables[5]  = createButtonDockable("ButtonDockableRefresh",         "Refresh",           new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/refresh.png")),           "Refresh!");
		buttonDockables[6]  = createButtonDockable("ButtonDockableBin",             "Bin",               new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/bin.png")),               "Bin!");
		buttonDockables[7]  = createButtonDockable("ButtonDockableIcons",           "Icons",             new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/icons.png")),             "Icons!");
		buttonDockables[8]  = createButtonDockable("ButtonDockableList",            "List",              new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/list.png")),              "List!");
		buttonDockables[9]  = createButtonDockable("ButtonDockableImages",           "Images",            new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/images.png")),            "Images!");
		buttonDockables[10] = createButtonDockable("ButtonDockableDivide",           "Divide",            new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/divide.png")),            "Divide!");
		buttonDockables[11] = createButtonDockable("ButtonDockableJoin",             "Join",              new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/join.png")),              "Join!");
		buttonDockables[12] = createButtonDockable("ButtonDockableSwitch",           "Switch",            new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/switch.png")),            "Switch!");
		buttonDockables[13] = createButtonDockable("ButtonDockableAsterisk",         "Asterisk",          new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/asterisk.png")),          "Asterisk!");
		buttonDockables[14] = createButtonDockable("ButtonDockableAnchor",           "Anchor",            new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/anchor.png")),            "Anchor!");
		buttonDockables[15] = createButtonDockable("ButtonDockableTerminal",         "Terminal",          new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/terminal.png")),          "Terminal!");		
		buttonDockables[16] = createButtonDockable("ButtonDockableStar",             "Well Done",         new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/star.png")),              "Well Done!");
		buttonDockables[17] = createButtonDockable("ButtonDockableWakeUp",           "Wake Up",           new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/wake_up.png")),           "Wake up!");
		buttonDockables[18] = createButtonDockable("ButtonDockableAddToBasket",      "Add to Basket",     new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/add_to_basket.png")),     "Add to Basket!");
		buttonDockables[19] = createButtonDockable("ButtonDockableRemoveFromBasket", "Remove from Basket",new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/remove_from_basket.png")),"Remove from Basket!");
		buttonDockables[20] = createButtonDockable("ButtonDockableBook",             "Book",              new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/book.png")),              "Book!");
		buttonDockables[21] = createButtonDockable("ButtonDockableBookPrevious",     "Previous Book",     new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/book_previous.png")),     "Previous Book!");
		buttonDockables[22] = createButtonDockable("ButtonDockableBookNext",         "Next Book",         new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/book_next.png")),         "Next Book!");
		buttonDockables[23] = createButtonDockable("ButtonDockableBookOpen",         "Open Book",         new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/book_open.png")),         "Open Book!");
		buttonDockables[24] = createButtonDockable("ButtonDockableBookEdit",         "Edit Book",         new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/book_edit.png")),         "Edit Book!");
		buttonDockables[25] = createButtonDockable("ButtonDockableBookAdd",          "Add Book",          new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/book_add.png")),          "Add Book!");
		buttonDockables[26] = createButtonDockable("ButtonDockableBookDelete",       "Delete Book",       new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/book_delete.png")),       "Delete Book!");
		buttonDockables[27] = createButtonDockable("ButtonDockableBookLink",         "Link Book",         new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/book_link.png")),         "Link Book!");
		buttonDockables[28] = createButtonDockable("ButtonDockableAttach",           "Attach",            new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/attach.png")),            "Attach!");
		buttonDockables[29] = createButtonDockable("ButtonDockableCalculator",       "Calculator",        new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/calculator.png")),        "Calculator!");
		buttonDockables[30] = createButtonDockable("ButtonDockableBriefcase",        "Briefcase",         new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/briefcase.png")),         "Briefcase!");
		buttonDockables[31] = createButtonDockable("ButtonDockableCalendar",         "Calendar",          new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/calendar.png")),          "Calendar!");
		buttonDockables[32] = createButtonDockable("ButtonDockableCamera",           "Camera",            new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/camera.png")),            "Camera!");
		buttonDockables[33] = createButtonDockable("ButtonDockableCar",              "Car",               new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/car.png")),               "Car!");
		buttonDockables[34] = createButtonDockable("ButtonDockableCD",               "CD",                new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/cd.png")),                "CD!");
		buttonDockables[35] = createButtonDockable("ButtonDockableClock",		    "Clock",              new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/clock.png")),             "Clock!");
		buttonDockables[36] = createButtonDockable("ButtonDockableCoins",            "Coins",             new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/coins.png")),             "Coins!");
		buttonDockables[37] = createButtonDockable("ButtonDockableChartBar",         "Bar Chart",         new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/chart_bar.png")),         "Bar Chart!");
		buttonDockables[38] = createButtonDockable("ButtonDockableChartCurve",       "Curve Chart",       new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/chart_curve.png")),       "Curve Chart!");
		buttonDockables[39] = createButtonDockable("ButtonDockableChartLine",        "Line Chart",        new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/chart_line.png")),        "Chart!");
		buttonDockables[40] = createButtonDockable("ButtonDockableChartOrganisation","Organisation Chart",new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/chart_organisation.png")),"Oraganisation Chart!");
		buttonDockables[41] = createButtonDockable("ButtonDockableChartPie",         "Pie Chart",         new ImageIcon(getClass().getResource("/com/javadocking/resources/icons/chart_pie.png")),         "Pie Chart!");

		// Try to decode the dock model from file.
		MyDockModelPropertiesDecoder dockModelDecoder = new MyDockModelPropertiesDecoder();
		if (dockModelDecoder.canDecodeSource(SOURCE))
		{
			try 
			{
				// Create the map with the dockables, that the decoder needs.
				Map<String, Dockable> dockablesMap = new HashMap<String, Dockable>();
				for (int index = 0; index < dockables.length; index++)
				{
					dockablesMap.put( dockables[index].getID(), dockables[index]);
				}	
				for (int index = 0; index < buttonDockables.length; index++)
				{
					dockablesMap.put( buttonDockables[index].getID(), buttonDockables[index]);
				}			
								
				// Create the map with the owner windows, that the decoder needs.
				Map<String, JFrame> ownersMap = new HashMap<String, JFrame>();
				ownersMap.put(frameId, frame);
				
				// Create the map with the visualizers, that the decoder needs.
				Map<String, Visualizer> visualizersMap = new HashMap<String, Visualizer>();
				visualizersMap.put("maximizer", maximizer);
				visualizersMap.put("minimizer", minimizer);
				visualizersMap.put("externalizer", externalizer);

				// Decode the file.
				dockModel = (FloatDockModel)dockModelDecoder.decode(SOURCE, dockablesMap, ownersMap, visualizersMap);
			}
			catch (FileNotFoundException fileNotFoundException){
				System.out.println("Could not find the file [" + SOURCE + "] with the saved dock model.");
				System.out.println("Continuing with the default dock model.");
			}
			catch (IOException ioException){
				System.out.println("Could not decode a dock model: [" + ioException + "].");
				ioException.printStackTrace();
				System.out.println("Continuing with the default dock model.");
			}
		}

		// These are the root docks.
		SplitDock totalSplitDock = null;
		BorderDock toolBarBorderDock = null;
		BorderDock minimizerBorderDock = null;
		
		DockingPath centerDockingPath = null;
		if (dockModel == null)	{

			// Create the dock model for the docks because they could not be retrieved from a file.
			dockModel = new FloatDockModel(SOURCE);
			dockModel.addOwner(frameId, frame);
			
			// Give the dock model to the docking manager.
			DockingManager.setDockModel(dockModel);
	
			// Create the tab docks.
			TabDock centerTabbedDock = new TabDock();
			TabDock bottomTabbedDock = new TabDock();
			TabDock leftTabbedDock = new TabDock();
			TabDock rightTabbedDock = new TabDock();
			
			// Add the dockables to these tab docks.
			centerTabbedDock.addDockable(dockables[0], new Position(0));
			centerTabbedDock.addDockable(dockables[1], new Position(1));
			centerTabbedDock.addDockable(dockables[2], new Position(2));
			centerTabbedDock.setSelectedDockable(dockables[0]);
			bottomTabbedDock.addDockable(dockables[11], new Position(0));
			leftTabbedDock.addDockable(dockables[9], new Position(0));
			rightTabbedDock.addDockable(dockables[12], new Position(0));

			// The 4 windows have to be splittable.
			SplitDock centerSplitDock = new SplitDock();
			centerSplitDock.addChildDock(centerTabbedDock, new Position(Position.CENTER));
			centerSplitDock.addChildDock(rightTabbedDock, new Position(Position.RIGHT));
			centerSplitDock.setDividerLocation(500);

			SplitDock bottomSplitDock = new SplitDock();
			bottomSplitDock.addChildDock(bottomTabbedDock, new Position(Position.CENTER));

			SplitDock rightSplitDock = new SplitDock();
			rightSplitDock.addChildDock(centerSplitDock, new Position(Position.CENTER));
			rightSplitDock.addChildDock(bottomSplitDock, new Position(Position.BOTTOM));
			rightSplitDock.setDividerLocation(380);

			SplitDock leftSplitDock = new SplitDock();
			leftSplitDock.addChildDock(leftTabbedDock, new Position(Position.CENTER));

			totalSplitDock = new SplitDock();
			totalSplitDock.addChildDock(leftSplitDock, new Position(Position.LEFT));
			totalSplitDock.addChildDock(rightSplitDock, new Position(Position.RIGHT));
			totalSplitDock.setDividerLocation(180);
				
			// When this SplitDock has only one child that is empty, it will not be removed.
			// This SplitDock will never be empty and will never be removed.
			// This is the main panel of the application where the windows of the application will be added.
			centerSplitDock.setRemoveLastEmptyChild(false);

			// Add the root dock to the dock model.
			dockModel.addRootDock("totalDock", totalSplitDock, frame);

			// Dockable 10 should float. Add dockable 10 to the float dock of the dock model (this is a default root dock).
			FloatDock floatDock = dockModel.getFloatDock(frame);
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			floatDock.addDockable(dockables[10], new Point(screenSize.width / 2 + 100, screenSize.height / 2 + 60),new Point());

			// Add the maximizer to the dock model.
			dockModel.addVisualizer("maximizer", maximizer, frame);
			
			// Create the border dock of the minimizer.
			minimizerBorderDock = new BorderDock(new ToolBarDockFactory());
			minimizerBorderDock.setMode(BorderDock.MODE_MINIMIZE_BAR);
			minimizerBorderDock.setCenterComponent(maximizer);
			borderDocker.setBorderDock(minimizerBorderDock);
	
			// Add the minimizer to the dock model.
			dockModel.addVisualizer("minimizer", minimizer, frame);
			
			// Create the tool bar border dock for the buttons.
			toolBarBorderDock = new BorderDock(new CompositeToolBarDockFactory(), minimizerBorderDock);
			toolBarBorderDock.setMode(BorderDock.MODE_TOOL_BAR);
			CompositeLineDock compositeToolBarDock1= new CompositeLineDock(CompositeLineDock.ORIENTATION_HORIZONTAL, false,
					new ToolBarDockFactory(), DockingMode.HORIZONTAL_TOOLBAR, DockingMode.VERTICAL_TOOLBAR);
			CompositeLineDock compositeToolBarDock2 = new CompositeLineDock(CompositeLineDock.ORIENTATION_VERTICAL, false,
					new ToolBarDockFactory(), DockingMode.HORIZONTAL_TOOLBAR, DockingMode.VERTICAL_TOOLBAR);
			toolBarBorderDock.setDock(compositeToolBarDock1, Position.TOP);
			toolBarBorderDock.setDock(compositeToolBarDock2, Position.LEFT);

			// Add this dock also as root dock to the dock model.
			dockModel.addRootDock("toolBarBorderDock", toolBarBorderDock, frame);
			
			// The line docks for the buttons.
			LineDock toolBarDock1 = new LineDock(LineDock.ORIENTATION_HORIZONTAL, false, DockingMode.HORIZONTAL_TOOLBAR, DockingMode.VERTICAL_TOOLBAR);
			LineDock toolBarDock2 = new LineDock(LineDock.ORIENTATION_HORIZONTAL, false, DockingMode.HORIZONTAL_TOOLBAR, DockingMode.VERTICAL_TOOLBAR);
			LineDock toolBarDock3 = new LineDock(LineDock.ORIENTATION_HORIZONTAL, false, DockingMode.HORIZONTAL_TOOLBAR, DockingMode.VERTICAL_TOOLBAR);
			LineDock toolBarDock4 = new LineDock(LineDock.ORIENTATION_HORIZONTAL, false, DockingMode.HORIZONTAL_TOOLBAR, DockingMode.VERTICAL_TOOLBAR);
			LineDock toolBarDock5 = new LineDock(LineDock.ORIENTATION_VERTICAL,   false, DockingMode.HORIZONTAL_TOOLBAR, DockingMode.VERTICAL_TOOLBAR);
			LineDock toolBarDock6 = new LineDock(LineDock.ORIENTATION_HORIZONTAL, false, DockingMode.HORIZONTAL_TOOLBAR, DockingMode.VERTICAL_TOOLBAR);
			LineDock toolBarDock7 = new LineDock(LineDock.ORIENTATION_VERTICAL,   false, DockingMode.HORIZONTAL_TOOLBAR, DockingMode.VERTICAL_TOOLBAR);
			LineDock toolBarDock8 = new LineDock(LineDock.ORIENTATION_VERTICAL,   false, DockingMode.HORIZONTAL_TOOLBAR, DockingMode.VERTICAL_TOOLBAR);
			
			// Add the button dockables to the line docks.
			toolBarDock1.addDockable(buttonDockables[0],  new Position(0));
			toolBarDock1.addDockable(buttonDockables[1],  new Position(1));
			toolBarDock1.addDockable(buttonDockables[2],  new Position(2));
			toolBarDock1.addDockable(buttonDockables[3],  new Position(3));
			toolBarDock1.addDockable(buttonDockables[4],  new Position(4));
			toolBarDock1.addDockable(buttonDockables[5],  new Position(5));
			toolBarDock1.addDockable(buttonDockables[6],  new Position(6));
			toolBarDock2.addDockable(buttonDockables[7],  new Position(0));
			toolBarDock2.addDockable(buttonDockables[8],  new Position(1));
			toolBarDock2.addDockable(buttonDockables[9],  new Position(2));
			toolBarDock3.addDockable(buttonDockables[10], new Position(0));
			toolBarDock3.addDockable(buttonDockables[11], new Position(1));
			toolBarDock3.addDockable(buttonDockables[12], new Position(2));
			toolBarDock4.addDockable(buttonDockables[13], new Position(0));
			toolBarDock4.addDockable(buttonDockables[14], new Position(1));
			toolBarDock4.addDockable(buttonDockables[15], new Position(2));
			toolBarDock4.addDockable(buttonDockables[16], new Position(3));
			toolBarDock4.addDockable(buttonDockables[17], new Position(4));
			toolBarDock5.addDockable(buttonDockables[18], new Position(0));
			toolBarDock5.addDockable(buttonDockables[19], new Position(1));
			toolBarDock6.addDockable(buttonDockables[20], new Position(0));
			toolBarDock6.addDockable(buttonDockables[21], new Position(1));
			toolBarDock6.addDockable(buttonDockables[22], new Position(2));
			toolBarDock6.addDockable(buttonDockables[23], new Position(3));
			toolBarDock6.addDockable(buttonDockables[24], new Position(4));
			toolBarDock6.addDockable(buttonDockables[25], new Position(5));
			toolBarDock6.addDockable(buttonDockables[26], new Position(6));
			toolBarDock6.addDockable(buttonDockables[27], new Position(7));
			toolBarDock6.addDockable(buttonDockables[28], new Position(8));
			toolBarDock7.addDockable(buttonDockables[29], new Position(0));
			toolBarDock7.addDockable(buttonDockables[30], new Position(1));
			toolBarDock7.addDockable(buttonDockables[31], new Position(2));
			toolBarDock7.addDockable(buttonDockables[32], new Position(3));
			toolBarDock7.addDockable(buttonDockables[33], new Position(4));
			toolBarDock7.addDockable(buttonDockables[34], new Position(5));
			toolBarDock7.addDockable(buttonDockables[35], new Position(6));
			toolBarDock7.addDockable(buttonDockables[36], new Position(6));
			toolBarDock8.addDockable(buttonDockables[37], new Position(0));
			toolBarDock8.addDockable(buttonDockables[38], new Position(1));
			toolBarDock8.addDockable(buttonDockables[39], new Position(2));
			toolBarDock8.addDockable(buttonDockables[40], new Position(3));
			toolBarDock8.addDockable(buttonDockables[41], new Position(4));

//			 Add the button line docks to their composite parents.
			compositeToolBarDock1.addChildDock(toolBarDock1, new Position(0));
			compositeToolBarDock1.addChildDock(toolBarDock2, new Position(1));
			compositeToolBarDock1.addChildDock(toolBarDock3, new Position(2));
			compositeToolBarDock1.addChildDock(toolBarDock4, new Position(3));  
			compositeToolBarDock2.addChildDock(toolBarDock5, new Position(0));
			compositeToolBarDock2.addChildDock(toolBarDock7, new Position(1));
			compositeToolBarDock2.addChildDock(toolBarDock8, new Position(2));
			floatDock.addChildDock(toolBarDock6, new Position(screenSize.width / 2 + 100, screenSize.height / 2 - 170));

			// Minimize dockable 3, 4, 5, 6, 7.
			minimizer.visualizeDockable(dockables[3]);
			minimizer.visualizeDockable(dockables[4]);
			minimizer.visualizeDockable(dockables[5]);
			minimizer.visualizeDockable(dockables[6]);
			minimizer.visualizeDockable(dockables[7]);
			
			// Add an externalizer to the dock model.
			dockModel.addVisualizer("externalizer", externalizer, frame);

			// Add the paths of the docked dockables to the model with the docking paths.
			addDockingPath(dockables[0]);
			addDockingPath(dockables[1]);
			addDockingPath(dockables[2]);
			addDockingPath(dockables[9]);
			addDockingPath(dockables[10]);
			addDockingPath(dockables[11]);
			addDockingPath(dockables[12]);

			// Add the path of the dockables that are not docked already.
			// We want dockable 8 to be docked, when it is made visible, where dockable 11 is docked.
			DockingPath dockingPathToCopy11 = DockingManager.getDockingPathModel().getDockingPath(dockables[11].getID());
			DockingPath dockingPath8 = DefaultDockingPath.copyDockingPath(dockables[8], dockingPathToCopy11);
			DockingManager.getDockingPathModel().add(dockingPath8);
			// We want dockable 3, 4, 5, 6, 7 to be docked, when they are made visible, where dockable 0 is docked.
			DockingPath dockingPathToCopy1 = DockingManager.getDockingPathModel().getDockingPath(dockables[0].getID());
			DockingPath dockingPath3 = DefaultDockingPath.copyDockingPath(dockables[3], dockingPathToCopy1);
			DockingPath dockingPath4 = DefaultDockingPath.copyDockingPath(dockables[4], dockingPathToCopy1);
			DockingPath dockingPath5 = DefaultDockingPath.copyDockingPath(dockables[5], dockingPathToCopy1);
			DockingPath dockingPath6 = DefaultDockingPath.copyDockingPath(dockables[6], dockingPathToCopy1);
			DockingPath dockingPath7 = DefaultDockingPath.copyDockingPath(dockables[7], dockingPathToCopy1);
			DockingManager.getDockingPathModel().add(dockingPath3);
			DockingManager.getDockingPathModel().add(dockingPath4);
			DockingManager.getDockingPathModel().add(dockingPath5);
			DockingManager.getDockingPathModel().add(dockingPath6);
			DockingManager.getDockingPathModel().add(dockingPath7);
			
			// The docking path where very new windows will be placed.
			centerDockingPath = DefaultDockingPath.copyDockingPath(CENTER_DOCKING_PATH_ID, DockingManager.getDockingPathModel().getDockingPath(dockables[0].getID()));

		}
		else
		{
			
			// Get the root dock from the dock model.
			totalSplitDock = (SplitDock)dockModel.getRootDock("totalDock");
			toolBarBorderDock = (BorderDock)dockModel.getRootDock("toolBarBorderDock");
			
			// Get the border dock of the minimizer. Set it as border dock to the border docker.
			minimizerBorderDock = (BorderDock)toolBarBorderDock.getChildDockOfPosition(Position.CENTER);
			minimizerBorderDock.setCenterComponent(maximizer);
			borderDocker.setBorderDock(minimizerBorderDock);
			
			// Get the docking path where very new windows have to be docked.
			centerDockingPath = dockModelDecoder.getCenterDockingPath();
		}

		// Listen when the frame is closed. The workspace should be saved.
		frame.addWindowListener(new WorkspaceSaver(centerDockingPath));

		// Add the content to the maximize panel.
		maximizer.setContent(totalSplitDock);
						
		// Add the border dock of the minimizer to the panel.
		add(toolBarBorderDock, BorderLayout.CENTER);
		
		// Create the menubar.
		JMenuBar menuBar = createMenu(dockables, centerDockingPath);
		frame.setJMenuBar(menuBar);

	}
	
	private Dockable createDockable(String id, Component content, String title, Icon icon, String description)
	{
		
		// Create the dockable.
		DefaultDockable dockable = new DefaultDockable(id, content, title, icon);
		
		// Add a description to the dockable. It will be displayed in the tool tip.
		dockable.setDescription(description);
		
		return dockable;
		
	}
	private Dockable addAllActions(Dockable dockable)
	{
		
		Dockable wrapper = new StateActionDockable(dockable, new DefaultDockableStateActionFactory(), DockableState.statesClosed());
		wrapper = new StateActionDockable(wrapper, new DefaultDockableStateActionFactory(), DockableState.statesAllExceptClosed());
		return wrapper;

	}
	private Dockable addLimitActions(Dockable dockable)
	{
		
		Dockable wrapper = new StateActionDockable(dockable, new DefaultDockableStateActionFactory(), DockableState.statesClosed());
		int[] limitStates = {DockableState.NORMAL, DockableState.MINIMIZED, DockableState.EXTERNALIZED};
		wrapper = new StateActionDockable(wrapper, new DefaultDockableStateActionFactory(), limitStates);
		return wrapper;

	}
	private Dockable createButtonDockable(String id, String title, Icon icon, String message)
	{
		
		// Create the action.
		MessageAction action = new MessageAction(this, title, icon, message);
		ToolBarButton button = new ToolBarButton(action);
		// Create the dockable with the button as component.
		ButtonDockable buttonDockable = new ButtonDockable(id, button);

		// Add a dragger to the individual dockable.
		createDockableDragger(buttonDockable);

		return buttonDockable;
		
	}
	private Dockable createButtonDockable1(String id, String title, Icon icon, String message)
	{
		
		// Create the action.
		ClickAction action = new ClickAction(this, title, icon, message);
		ToolBarButton button = new ToolBarButton(action);
		// Create the dockable with the button as component.
		ButtonDockable buttonDockable = new ButtonDockable(id, button);

		// Add a dragger to the individual dockable.
		createDockableDragger(buttonDockable);

		return buttonDockable;
		
	}
	private void createDockableDragger(Dockable dockable) {
		
		// Create the dragger for the dockable.
		DragListener dragListener = DockingManager.getDockableDragListenerFactory().createDragListener(dockable);
		dockable.getContent().addMouseListener(dragListener);
		dockable.getContent().addMouseMotionListener(dragListener);
		
	}

	private JMenuBar createMenu(Dockable[] dockables, DockingPath centerDockingPath)
	{
		// Create the menu bar.
		JMenuBar menuBar = new JMenuBar();

		// Build the File menu.
		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic(KeyEvent.VK_F);
		fileMenu.getAccessibleContext().setAccessibleDescription("File");
		menuBar.add(fileMenu);
		
		// Build the Window menu.
		JMenu windowMenu = new JMenu("Quản Lý");
		windowMenu.setMnemonic(KeyEvent.VK_W);
		windowMenu.getAccessibleContext().setAccessibleDescription("The Window Menu");
		menuBar.add(windowMenu);
		
		// Build the Look and Feel menu.
		JMenu lookAndFeelMenu = new JMenu("Tác Vụ");
		lookAndFeelMenu.setMnemonic(KeyEvent.VK_L);
		lookAndFeelMenu.getAccessibleContext().setAccessibleDescription("The Lool and Feel Menu");
		menuBar.add(lookAndFeelMenu);
		
		// Build the Dragging menu.
		JMenu help = new JMenu("Trợ Giúp");
		//MenuItem item = new MenuItem("Liên hệ");
		help.setMnemonic(KeyEvent.VK_D);
		help.getAccessibleContext().setAccessibleDescription("The Dragging Menu");
		menuBar.add(help);

		// The JMenuItem for File
		JMenuItem menuItem = new JMenuItem("Thoát", KeyEvent.VK_E);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.ALT_MASK));
		menuItem.getAccessibleContext().setAccessibleDescription("Exit the application");
		menuItem.addActionListener(new ActionListener()

				{
					public void actionPerformed(ActionEvent arg0)

					{
						System.exit(0);
					}
				});
		fileMenu.add(menuItem);

		// The JMenuItems for the dockables.
		for (int index = 0; index < dockables.length; index++)
		{
			// Create the check box menu for the dockable.
			JCheckBoxMenuItem cbMenuItem = new DockableMenuItem(dockables[index]);
			windowMenu.add(cbMenuItem);			
		}
		
		windowMenu.addSeparator();
		windowMenu.add(new JMenuItem(new NewDockableAction(centerDockingPath)));
		
		// One JMenuItem for a very new window in a dockable.--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		windowMenu.addSeparator();
		menuItem = new JMenuItem("Hàng hóa", KeyEvent.VK_H);
		windowMenu.add(menuItem);
		menuItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				hh.xoa();
				hh.setVisible(true);
			}
		});
		windowMenu.addSeparator();
		JMenuItem menuItem1 = new JMenuItem("Đầu kỳ", KeyEvent.VK_H);
		windowMenu.add(menuItem1);
		menuItem1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				dk.xoa();
				dk.setVisible(true);
			}
		});
		// The JMenuItems for the look and feels.
		ButtonGroup group = new ButtonGroup();
		for (int index = 0; index < LAFS.length; index++)

		{
			LafMenuItem lafMenuItem = new LafMenuItem(LAFS[index]);
			lookAndFeelMenu.add(lafMenuItem);
			group.add(lafMenuItem);
		}

		return menuBar;
	} 
	private DockingPath addDockingPath(Dockable dockable)
	{

		if (dockable.getDock() != null)
		{
			// Create the docking path of the dockable.
			DockingPath dockingPath = DefaultDockingPath.createDockingPath(dockable);
			DockingManager.getDockingPathModel().add(dockingPath);
			return dockingPath;
		}

		return null;

	}

	private void saveWorkspace(DockingPath centerDockingPath)
	{
		
		// Save the dock model.
		DockModelPropertiesEncoder encoder = new MyDockModelPropertiesEncoder(centerDockingPath);
		if (encoder.canSave(dockModel))
		{
			try
			{
				encoder.save(dockModel);
			}
			catch (Exception e)
			{
				System.out.println("Error while saving the dock model.");
				e.printStackTrace();
			}
		}
		else
		{
			System.out.println("Could not save the dock model.");
		}

	}
	

	/**
	 * An action that shows a message in a dialog.
	 */
	private class MessageAction extends AbstractAction	{

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private Component parentComponent;
		private String message;
		private String name;
		
		public MessageAction(Component parentComponent, String name, Icon icon, String message)
		{
			super(null, icon);

 			putValue(Action.SHORT_DESCRIPTION, name);
			this.name = name;
 			this.parentComponent = parentComponent;
			
		}

		@Override
		public void actionPerformed(ActionEvent actionEvent)
		{
				JOptionPane.showMessageDialog(parentComponent,
						message, name, JOptionPane.INFORMATION_MESSAGE);
		}
		
	}
	private class ClickAction extends AbstractAction	{

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		String name;
		public ClickAction(Component parentComponent, String name, Icon icon, String message)
		{
			super(null, icon);
			this.name=name;
		}

		@Override
		public void actionPerformed(ActionEvent e)
		{
			if(name.equals("Delete")){
				if(mokhanhoa){
					hh.delete();
				}
				if(mokdauky){
					dk.delete();
				}
			}
			if(name.equals("Print")){
				if(mokhanhoa){
					try{
						InputStream inputStream = new FileInputStream ("reports/report1.jrxml");
						JRBeanCollectionDataSource beanColDataSource = new	JRBeanCollectionDataSource(listmaterial.list);
						 
						Map<String, Object> parameters = new HashMap<String, Object>();
						JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
						JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
						JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
	//					JasperExportManager.exportReportToPdfFile(jasperPrint, "D:/javaExample/QLVT/reports/listTableHanghoa.pdf");
						JasperViewer jv = new JasperViewer(jasperPrint, false);
				        jv.setTitle("Report");
				        jv.setVisible(true);
				        
					}catch(Exception ex){
							ex.printStackTrace();
						}
				}
				if(mokdauky){
					try{
						InputStream inputStream1 = new FileInputStream ("reports/report2.jrxml");
						JRBeanCollectionDataSource beanColDataSource1 = new	JRBeanCollectionDataSource(listdauky.list);
						 
						Map<String, Object> parameters1 = new HashMap<String, Object>();
						JasperDesign jasperDesign1 = JRXmlLoader.load(inputStream1);
						JasperReport jasperReport1 = JasperCompileManager.compileReport(jasperDesign1);
						JasperPrint jasperPrint1 = JasperFillManager.fillReport(jasperReport1, parameters1, beanColDataSource1);
	//					JasperExportManager.exportReportToPdfFile(jasperPrint, "D:/javaExample/QLVT/reports/listTableHanghoa.pdf");
						JasperViewer jv1 = new JasperViewer(jasperPrint1, false);
				        jv1.setTitle("Report");
				        jv1.setVisible(true);
				        
					}catch(Exception ex){
							ex.printStackTrace();
						}
				}
			}
		}
	}

	/**
	 * A listener for window closing events. Saves the workspace, when the application window is closed.
	 * 
	 * @author Heidi Rakels.
	 */
	private class WorkspaceSaver implements WindowListener
	{
		private DockingPath dockingPath;
		
		private WorkspaceSaver(DockingPath centerDockingPath) {
			if (centerDockingPath != null) 
			{
				this.dockingPath = DefaultDockingPath.copyDockingPath("centerDockingPath", centerDockingPath);
			}
		}

		@Override
		public void windowClosing(WindowEvent windowEvent) 
		{
			saveWorkspace(dockingPath);
		}

		@Override public void windowDeactivated(WindowEvent windowEvent) {}
		@Override public void windowDeiconified(WindowEvent windowEvent) {}
		@Override public void windowIconified(WindowEvent windowEvent) {}
		@Override public void windowOpened(WindowEvent windowEvent) {}
		@Override public void windowActivated(WindowEvent windowEvent) {}
		@Override public void windowClosed(WindowEvent windowEvent) {}

	}

	/**
	 * A check box menu item to add or remove the dockable.
	 */
	private class DockableMenuItem extends JCheckBoxMenuItem
	{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public DockableMenuItem(Dockable dockable)
		{
			super(dockable.getTitle(), dockable.getIcon());
			
			setSelected(dockable.getDock() != null);
			
			DockableMediator dockableMediator = new DockableMediator(dockable, this);
			dockable.addDockingListener(dockableMediator);
			addItemListener(dockableMediator);
		}
		
	}
	
	
	/**
	 * An action to create a new dockable.
	 */
	private class NewDockableAction extends AbstractAction {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private DockingPath centerDockingPath;
		public NewDockableAction(DockingPath centerDockingPath)
		{
			super("Create a very new window in a Dockable");
			this.centerDockingPath = centerDockingPath;
		}

		public void actionPerformed(ActionEvent actionEvent) {
			Dockable dockable = createDockable("new_window" + newDockableCount++, 	 new TextPanel("I am window " + newDockableCount + "."),      "I am very new dockable " + newDockableCount,  new ImageIcon(getClass().getResource("/com/javadocking/resources/images/hello12.gif")),     "This is a dockable that was created when the user clicked an action");		
			if (centerDockingPath != null) {
				DockingPath dockingPath = DefaultDockingPath.copyDockingPath(dockable, centerDockingPath);
				DockingManager.getDockingExecutor().changeDocking(dockable, dockingPath);
			} else {
				// Get the root dock.
				SplitDock totalSplitDock = (SplitDock)dockModel.getRootDock("totalDock");
				DockingManager.getDockingExecutor().changeDocking(dockable, totalSplitDock);
			}
		}
	}
	
	/**
	 * This is the class for the content.
	 */
	private class TextPanel extends JPanel implements DraggableContent
	{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private JLabel label;
		
		public TextPanel(String text)
		{
			super(new FlowLayout());
			
			// The panel.
			setMinimumSize(new Dimension(80,80));
			setPreferredSize(new Dimension(150,150));
			setBackground(Color.white);
			setBorder(BorderFactory.createLineBorder(Color.lightGray));
			
			// The label.
			label = new JLabel(text);
			label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
			add(label);
		}
		
		// Implementations of DraggableContent.

		public void addDragListener(DragListener dragListener)
		{
			addMouseListener(dragListener);
			addMouseMotionListener(dragListener);
			label.addMouseListener(dragListener);
			label.addMouseMotionListener(dragListener);
		}
	}

	/**
	 * A check box menu item to enable a look and feel.
	 */

	private class LafMenuItem extends JRadioButtonMenuItem
	{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public LafMenuItem(LAF laf)
		{
			super(laf.getTitle());
			
			// Is this look and feel supported?
			if (laf.isSupported())
			{
				LafListener lafItemListener = new LafListener(laf);
				addActionListener(lafItemListener);
			}
			else
			{
				setEnabled(false);
			}
			
			if (laf.isSelected())
			{
				setSelected(true);
			}
		}
	}
	
	/**
	 * A listener that installs its look and feel.
	 */
	private class LafListener implements ActionListener
	{

		// Fields.

		private LAF laf;
		
		// Constructors.

		public LafListener(LAF laf)
		{
			this.laf = laf;
		}
		
		// Implementations of ItemListener.

		public void actionPerformed(ActionEvent arg0)
		{
			for (int index = 0; index < LAFS.length; index++)
			{
				LAFS[index].setSelected(false);
			}
//			setLookAndFeel(laf);
			laf.setSelected(true);
		}
		
	}
	
	/**
	 * A listener that listens when menu items with dockables are selected and deselected.
	 * It also listens when dockables are closed or docked.
	 */
	private class DockableMediator implements ItemListener, DockingListener
	{
		
		private Dockable dockable;
		private Action closeAction;
		private Action restoreAction;
		private JMenuItem dockableMenuItem;
		public DockableMediator(Dockable dockable, JMenuItem dockableMenuItem) 
		{
			
			this.dockable = dockable;
			this.dockableMenuItem = dockableMenuItem;
			closeAction = new DefaultDockableStateAction(dockable, DockableState.CLOSED);
			restoreAction = new DefaultDockableStateAction(dockable, DockableState.NORMAL);

		}

		public void itemStateChanged(ItemEvent itemEvent)
		{
			
			dockable.removeDockingListener(this);
			if (itemEvent.getStateChange() == ItemEvent.DESELECTED)
			{
				// Close the dockable.
				if( dockable.equals(dockables[1])){
					mok=false;
					closeAction.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Close"));
					floatDock.ghostChild(toolGridDock);
				}else
				closeAction.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Close"));
			} 
			else 
			{
				// Restore the dockable.
				if( dockable.equals(dockables[1])){
					if(mok==false){
						mok=true;
						floatDock.addChildDock(toolGridDock, new Position(600,50));
					}
					restoreAction.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Restore"));
				}else
				restoreAction.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Restore"));
			}
			dockable.addDockingListener(this);
			
		}

		public void dockingChanged(DockingEvent dockingEvent) {
			if (dockingEvent.getDestinationDock() != null)
			{
				dockableMenuItem.removeItemListener(this);
				dockableMenuItem.setSelected(true);
				dockableMenuItem.addItemListener(this);	
			}
			else
			{
				dockableMenuItem.removeItemListener(this);
				dockableMenuItem.setSelected(false);
				dockableMenuItem.addItemListener(this);
			}
		}

		public void dockingWillChange(DockingEvent dockingEvent) {}



	}
	
	private class WorkspaceComponentFactory extends SampleComponentFactory	{

		public DockHeader createDockHeader(LeafDock dock, int orientation)	{
			return new PointDockHeader(dock, orientation);
		}
		
	}
	
	private class MyDockModelPropertiesEncoder extends DockModelPropertiesEncoder {
		private DockingPath centerDockingPath;
		
		private MyDockModelPropertiesEncoder(DockingPath centerDockingPath) {
			this.centerDockingPath = centerDockingPath;
		}

		protected void saveProperties(DockModel dockModel, DockingPathModel dockingPathModel, Properties properties, Map dockKeys) {
			super.saveProperties(dockModel, dockingPathModel, properties, dockKeys);
			if (centerDockingPath != null)	{
				centerDockingPath.saveProperties(CENTER_DOCKING_PATH_ID, properties, dockKeys);
			}
		}
		
	}
	
	private class MyDockModelPropertiesDecoder extends DockModelPropertiesDecoder	{
		private DockingPath centerDockingPath;
		protected DockModel decodeProperties(Properties properties, String sourceName, Map dockablesMap, Map ownersMap, Map visualizersMap, Map docks) throws IOException 
		{
			DockModel dockModel = super.decodeProperties(properties, sourceName, dockablesMap, ownersMap, visualizersMap, docks);
			if (dockModel != null) {
				centerDockingPath = new DefaultDockingPath();
				centerDockingPath.loadProperties(CENTER_DOCKING_PATH_ID, properties, docks);
				if (centerDockingPath.getID() == null) {
					System.out.println("The file 'workspace_1_5.dck' of an older version is still available, remove this file.");
					centerDockingPath = null;
				}
			}
			
			return dockModel;
		}
		public DockingPath getCenterDockingPath() 
		{
			return centerDockingPath;
		}
		
	}

	// Main method.

	public static void createAndShowGUI()
	{ 
		
		// Create the look and feels.
		LAFS  = new LAF[1];	
		LAFS[0] = new LAF("Windows", "com.sun.java.swing.plaf.windows.WindowsLookAndFeel", LAF.THEME_DEAULT);

		// Set the first enabled look and feel.
		try 
		{
				if (LAFS[0].isSupported())
				{
					LAFS[0].setSelected(true);
					UIManager.setLookAndFeel(LAFS[0].getClassName());
				}
		} catch (Exception e) { }

		// Remove the borders from the split panes and the split pane dividers.
		LookAndFeelUtil.removeAllSplitPaneBorders();

		// Create the frame.
		JFrame frame = new JFrame("Workspace Example");
		
		// Set the default location and size.
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

		frame.setLocation((screenSize.width - FRAME_WIDTH) / 2, (screenSize.height - FRAME_HEIGHT) / 2);

		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);

		// Create the panel and add it to the frame.
		MainForm panel = new MainForm(frame);
		frame.getContentPane().add(panel);
		
		// Set the frame properties and show it.
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
	}

	public static void main(String args[]) {
		
        Runnable doCreateAndShowGUI = new Runnable() {
        	
            public void run() {
            	
                createAndShowGUI();
            }
        };
        SwingUtilities.invokeLater(doCreateAndShowGUI);
    }


	
}
